import 'package:dali_ng_pera/page/browser/dnp_browser_page.dart';
import 'package:dali_ng_pera/page/certification/dnp_certification_page.dart';
import 'package:dali_ng_pera/page/home/model/dnp_home_data_model.dart';
import 'package:get/get.dart';

mixin DnpHomeHeardMixin {
  DnpSyllable? getSyllable(DnpHomeDataModel model,String heard) {
    for (var syllable in model.syllables ?? <DnpSyllable>[]) {
      if (syllable.heard == heard) {
        return syllable;
      }
    }
    return null;
  }

  DnpCitizen? getCitizen(DnpHomeDataModel model,String heard) {
    List<DnpCitizen> citizens = getSyllable(model,heard)?.citizens ?? [];
    for (var citizen in citizens) {
      return citizen;
    }
    return null;
  }

  List<DnpCitizen> getProducts(DnpHomeDataModel model){
    DnpSyllable? curSyllable;
    for (var syllable in model.syllables ?? <DnpSyllable>[]) {
      if (syllable.heard == "od") {
        curSyllable = syllable;
        break;
      }
    }
    return curSyllable?.citizens ??[];
  }



  /// 解析URL参数
  /// 
  /// 例如: @ios://Dali.ngPe.ra/aisle?already=1&name=test
  /// 返回: {
  ///   'scheme': 'ios',
  ///   'host': 'Dali.ngPe.ra',
  ///   'path': '/aisle',
  ///   'queryParameters': {'already': '1', 'name': 'test'}
  /// }
  Map<String, dynamic> parseUrlParameters(String url) {
    // 移除开头的@符号（如果存在）
    String cleanUrl = url.trim();
    if (cleanUrl.startsWith('@')) {
      cleanUrl = cleanUrl.substring(1);
    }

    try {
      Uri uri = Uri.parse(cleanUrl);
      
      return {
        'scheme': uri.scheme,
        'host': uri.host,
        'path': uri.path,
        'queryParameters': uri.queryParameters,
      };
    } catch (e) {
      print('URL解析失败: $e');
      return {
        'scheme': '',
        'host': '',
        'path': '',
        'queryParameters': <String, String>{},
      };
    }
  }

  /// 获取URL中的查询参数
  /// 
  /// 例如: @ios://Dali.ngPe.ra/aisle?already=1&name=test
  /// 返回: {'already': '1', 'name': 'test'}
  Map<String, String> getQueryParameters(String url) {
    return parseUrlParameters(url)['queryParameters'] as Map<String, String>;
  }

  /// 获取URL中指定的参数值
  /// 
  /// 例如: getParameterValue('@ios://Dali.ngPe.ra/aisle?already=1', 'already')
  /// 返回: '1'
  String? getParameterValue(String url, String paramName) {
    Map<String, String> params = getQueryParameters(url);
    return params[paramName];
  }

  void handleJump(String src,String title) {
    // 解析URL参数
    Map<String, dynamic> urlInfo = parseUrlParameters(src);
    Map<String, String> params = urlInfo['queryParameters'] as Map<String, String>;
    
    if (src.startsWith("ios://Dali.ngPe.ra/alikelove")) {
      //设置
      print('参数: $params');
    } else if (src.startsWith("ios://Dali.ngPe.ra/notice")) {
      //首页
      print('参数: $params');
    } else if (src.startsWith("ios://Dali.ngPe.ra/girl")) {
      //登录
      print('参数: $params');
    } else if (src.startsWith("ios://Dali.ngPe.ra/her")) {
      //订单
      print('参数: $params');
    } else if (src.startsWith("ios://Dali.ngPe.ra/aisle")) {
      //产品详情
      print('参数: $params');
      if(params.containsKey("already")){
        Get.to(DnpCertificationPage(already: params['already']!));
      }
    }else if(src.isNotEmpty){
      Get.to(DnpBrowserPage(path: src));
    }
  }
}
